package com.queseraly.myweb.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.ListUtils;
import com.queseraly.myweb.component.DataReadListener;
import com.queseraly.myweb.entity.Data;
import com.queseraly.myweb.utils.DiffHandleUtils;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.apache.commons.codec.CharEncoding;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 测试控制类
 *
 * @author laiyong
 * @version v1.0
 * @since 2024/1/1
 */
@RestController
@RequestMapping(value = "/test")
@Validated
public class TestController {

    @PostMapping(value = "/import")
    public void importFromExcel(@RequestParam MultipartFile multipartFile) {
        try {
            EasyExcel.read(multipartFile.getInputStream(), Data.class, new DataReadListener<>(this::parseData)).sheet(0).doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping(value = "/export")
    public void exportToExcel(HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=write.xlsx");
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            // 分页查询数据
            EasyExcel.write(outputStream, Data.class)
                    .sheet("导出信息")
                    .doWrite(this::getData);
            outputStream.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping(value = "/compare")
    public void compare(HttpServletResponse response) {
        response.setContentType(MediaType.TEXT_HTML_VALUE);
        response.setCharacterEncoding(CharEncoding.UTF_8);
        try {
            List<String> diffString = DiffHandleUtils.diffString("D:\\Documentation\\txtCompare\\test1.json", "D:\\Documentation\\txtCompare\\test2.json");
            List<String> diffString1 = DiffHandleUtils.diffString("D:\\Documentation\\txtCompare\\test3.json", "D:\\Documentation\\txtCompare\\test4.json");
            List<String> diffString2 = DiffHandleUtils.diffString("D:\\Documentation\\txtCompare\\test1.json", "D:\\Documentation\\txtCompare\\test5.json");
            diffString.addAll(diffString2);
            for (int i = 0; i < 100; i++) {
                diffString.addAll(diffString1);
            }
            DiffHandleUtils.generateDiffHtml(diffString, response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public List<Data> getData() {
        List<Data> list = ListUtils.newArrayList();
        for (int i = 0; i < 10; i++) {
            Data data = new Data();
            data.setId(i);
            data.setName("aaa" + i);
            data.setAge(i + 15);
            data.setGender(i % 2 == 0 ? "男" : "女");
            data.setHobbyList(ListUtils.newArrayList("篮球", "足球", "羽毛球", "自行车"));
            list.add(data);
        }
        return list;
    }

    public void parseData(List<@Valid Data> dataList) {
        dataList.forEach(System.out::println);
    }
}
